package org.calculator;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

public class CustomCalculatorActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		List<String> buttons = this.parse();

		if (buttons != null) {
			for (String s : buttons) {
				Log.i("MAIN", s);
			}
		}

		Log.i("MAIN", "---end---");
	}

	public List<String> parse() {
		List<String> buttons = null;
		XmlResourceParser xrp = getResources().getXml(R.layout.main);
		try {
			int evento = xrp.getEventType();
			while (evento != XmlPullParser.END_DOCUMENT) {
				String etiqueta = null;

				switch (evento) {
				case XmlPullParser.START_DOCUMENT:
					buttons = new LinkedList<String>();
					break;

				case XmlPullParser.START_TAG:
					etiqueta = xrp.getName();

					if (etiqueta.equals("data")) {
						String id = xrp.getAttributeValue(0);
						// if (id != null)
						buttons.add(id);
						
						// else
						// buttons.add("NO_ID");
					}
					break;
				}

				evento = xrp.next();
			}
		} catch (NotFoundException e) {
			Log.e("MAIN", "NotFoundException: " + e);
		} catch (IOException e) {
			Log.e("MAIN", "IOException: " + e);
		} catch (XmlPullParserException e) {
			Log.e("MAIN", "XmlPullParserException: " + e);
		}

		return buttons;
	}
	
	private Button buildButton(String id) {
//		android:id="@+id/const_2"
//                android:layout_width="match_parent"
//                android:layout_height="wrap_content"
//                android:layout_weight="1.0"
//                android:text="@string/const_2"
		Button button = new Button(this.getApplicationContext());
		
		
		return null;
	}
}